#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 1e5 + 10, INF = 0x3f3f3f3f;

int n;
int w[N];
int dp[N][3];

int main()
{
    cin >> n;
    for (int i = 1; i <= n; i ++ )
        cin >> w[i];
    
    memset(dp, -INF, sizeof dp);
    dp[0][0] = 0;
    for (int i = 1; i <= n; i ++ )
    {
        dp[i][0] = max(dp[i - 1][0], dp[i - 1][2]); // 今天未持有 = 昨天未持有 ｜ 昨天最后一天冻结期
        dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - w[i]); // 今天持有 = 昨天持有 ｜ 昨天未持有 但昨天买入
        dp[i][2] = max(dp[i - 1][1] + w[i], dp[i - 1][2]); // 今天冻结 = 昨天开始冻结 ｜ 昨天卖出
    }
    int res = 0;
    for (int i = 0; i <= n; i ++ )
    {
        int t1 = max(dp[i][0], dp[i][1]);
        int t2 = max(res, dp[i][2]);
        res = max(t1, t2);
    }
    cout << res << endl;
    return 0;
}
